package edu.pe.cayetano.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.apache.commons.beanutils.DynaBean;

import ws.synopsis.frmwrk.common.BResult;
import edu.pe.cayetano.framework.Configuration;
import edu.pe.cayetano.framework.DAOException;
import edu.pe.cayetano.model.bean.DiagnosticoPresuntivoBean;
import edu.pe.cayetano.util.Utilitario;

public class DiagnosticoPresuntivoDAO extends BaseDAO{

	String 					schema			= Configuration.getSchemaDB();
	
	public DiagnosticoPresuntivoDAO(Connection cnx) {
		super(cnx);
	}

	public BResult buscarDiagnosticoPresuntivoEspecifica(String codigo) throws Exception {

		DiagnosticoPresuntivoBean 	diagPresTO 	= null;
		PreparedStatement   		pstmt 		 	= null;
		ResultSet 					rs 			 	= null;
		BResult						ret				= null;
		try{		
			StringBuffer query  = new StringBuffer();
			query.append(" SELECT");
			query.append(" FILEHC,ESTADO_GENERAL,TEJIDO_PERIODENTAL,TEJIDO_DENTAL,OCLUSION,OTROS"); 	
			query.append(" ,MODELO_ESTUDIO,RX_PERIAPICALES,RX_BITEWING,SET_FOTOGRAFICO"); 	
			query.append(" ,RX_PANORAMICA,RX_CEFALOMETRICA,RX_ATM,OTROS_PLANTRABAJO,INFORME_RADIOGRAFICO,COMENTARIO,ESTADO_ID");
			query.append(" FROM "+schema+"DIAGNOSTICO_PRESUNTIVO"); 
			query.append(" WHERE FILEHC=?");
			System.out.println("buscarDiagnosticoPresuntivoEspecifica:: buscarDiagnosticoPresuntivoEspecifica:: "+query.toString());
			
			pstmt = getConnection().prepareStatement(query.toString());
			pstmt.setString(1,codigo.substring(2, codigo.length()));
			rs=pstmt.executeQuery();
			ret = new BResult();
			if(rs.next()){
				diagPresTO=new DiagnosticoPresuntivoBean();
				diagPresTO.setFileHC(rs.getString(1));
				diagPresTO.setEstadoGeneral(rs.getString(2));
				diagPresTO.setTejidoPeriodental(rs.getString(3));
				diagPresTO.setTejidoDental(rs.getString(4));
				diagPresTO.setOclusion(rs.getString(5));
				diagPresTO.setOtros(rs.getString(6));
				diagPresTO.setModeloEstudio(rs.getString(7));
				diagPresTO.setRxPeriapicales(rs.getString(8));
				diagPresTO.setRxBitewing(rs.getString(9));
				diagPresTO.setSetFotografico(rs.getString(10));
				diagPresTO.setRxPanoramica(rs.getString(11));
				diagPresTO.setRxCefalometrica(rs.getString(12));
				diagPresTO.setRxATM(rs.getString(13));
				diagPresTO.setOtrosPlantrabajo(rs.getString(14));
				diagPresTO.setInformeRadiografico(rs.getString(15));
				diagPresTO.setComentario(rs.getString(16));
				diagPresTO.setEstadoID(rs.getInt(17));
				ret.setObject(diagPresTO);
				ret.setCode(0);
			}else{
				ret.setCode(1);
			}
		}catch(Exception e){
			ret = new BResult();
			ret.setCode(-1);
			ret.setMessage(e.getMessage());
		}finally{
			try{
				pstmt.close();
				rs.close();
			}
			catch(Exception e){
				throw new DAOException(e.getMessage());	
			}
		}
		return ret;
	}
	
	public BResult editaDiagnosticoPresuntivoArchivoHC(DynaBean dyna) throws Exception {
		BResult					ret				= null;
		PreparedStatement		pstmt 		 	= null;
		try{		
			StringBuffer query  = new StringBuffer();
			query.append(" UPDATE "+schema+"DIAGNOSTICO_PRESUNTIVO SET " );
			query.append(" ESTADO_GENERAL=?,TEJIDO_PERIODENTAL=?,TEJIDO_DENTAL=?,OCLUSION=?, ");            
			query.append(" OTROS=?,MODELO_ESTUDIO=?,RX_PERIAPICALES=?,RX_BITEWING=?,");         
			query.append(" SET_FOTOGRAFICO=?,RX_PANORAMICA=?,RX_CEFALOMETRICA=?,RX_ATM=?,");              
			query.append(" OTROS_PLANTRABAJO=?,INFORME_RADIOGRAFICO=? ");
			query.append(" WHERE FILEHC=?"); 
			String filehc = Utilitario.getPropertyDynaBean(dyna,"filehc");
			pstmt = getConnection().prepareStatement(query.toString());
			pstmt.setString(1,Utilitario.getPropertyDynaBean(dyna,"estadoGeneral"));
			pstmt.setString(2,Utilitario.getPropertyDynaBean(dyna,"tejidoPeriodental"));
			pstmt.setString(3,Utilitario.getPropertyDynaBean(dyna,"tejidoDental"));
			pstmt.setString(4,Utilitario.getPropertyDynaBean(dyna,"oclusion"));
			pstmt.setString(5,Utilitario.getPropertyDynaBean(dyna,"otros"));
			pstmt.setString(6,Utilitario.getPropertyDynaBean(dyna,"modeloEstudio"));
			pstmt.setString(7,Utilitario.getPropertyDynaBean(dyna,"rxPeriapicales"));
			pstmt.setString(8,Utilitario.getPropertyDynaBean(dyna,"rxBitewing"));
			pstmt.setString(9,Utilitario.getPropertyDynaBean(dyna,"setFotografico"));
			pstmt.setString(10,Utilitario.getPropertyDynaBean(dyna,"rxPanoramica"));
			pstmt.setString(11,Utilitario.getPropertyDynaBean(dyna,"rxCefalometrica"));
			pstmt.setString(12,Utilitario.getPropertyDynaBean(dyna,"rxATM"));
			pstmt.setString(13,Utilitario.getPropertyDynaBean(dyna,"otrosPlantrabajo"));
			pstmt.setString(14,Utilitario.getPropertyDynaBean(dyna,"informeRadiografico"));
			pstmt.setString(15,filehc.substring(2,filehc.length()));
			pstmt.executeUpdate();	
			ret = new BResult();
			ret.setCode(0);
		}catch(Exception e){
			ret = new BResult();
			ret.setCode(-1);
			ret.setMessage(e.getMessage());
		}finally{
			try{
				pstmt.close();
			}
			catch(Exception e){
				throw new DAOException(e.getMessage());	
			}
		}
		return ret;
	} 
	
	public BResult editaDiagnosticoPresuntivoArchivoHCxMedico(DynaBean dyna) throws Exception {
		BResult					ret				= null;
		PreparedStatement		pstmt 		 	= null;
		try{		
			StringBuffer query  = new StringBuffer();
			query.append(" UPDATE "+schema+"DIAGNOSTICO_PRESUNTIVO SET COMENTARIO=?,ESTADO_ID=? WHERE FILEHC=?"); 
			String filehc = Utilitario.getPropertyDynaBean(dyna,"filehc");
			pstmt = getConnection().prepareStatement(query.toString());
			pstmt.setString(1,Utilitario.getPropertyDynaBean(dyna,"comentario"));
			pstmt.setString(2,Utilitario.getPropertyDynaBean(dyna,"cboEstado"));
			pstmt.setString(3,filehc.substring(2,filehc.length()));
			pstmt.executeUpdate();	
			ret = new BResult();
			ret.setCode(0);
		}catch(Exception e){
			ret = new BResult();
			ret.setCode(-1);
			ret.setMessage(e.getMessage());
		}finally{
			try{
				pstmt.close();
			}
			catch(Exception e){
				throw new DAOException(e.getMessage());	
			}
		}
		return ret;
	} 
	
	public BResult insertaDiagnosticoPresuntivoArchivoHC(DynaBean dyna) throws Exception {

		BResult					ret				= null;
		PreparedStatement		pstmt 		 	= null;
		try{		
			StringBuffer query  = new StringBuffer();
			query.append(" INSERT INTO "+schema+"DIAGNOSTICO_PRESUNTIVO VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); 
			String filehc = Utilitario.getPropertyDynaBean(dyna,"filehc");
			pstmt = getConnection().prepareStatement(query.toString());
			pstmt.setString(1,filehc.substring(2,filehc.length()));
			pstmt.setString(2,Utilitario.getPropertyDynaBean(dyna,"estadoGeneral"));
			pstmt.setString(3,Utilitario.getPropertyDynaBean(dyna,"tejidoPeriodental"));
			pstmt.setString(4,Utilitario.getPropertyDynaBean(dyna,"tejidoDental"));
			pstmt.setString(5,Utilitario.getPropertyDynaBean(dyna,"oclusion"));
			pstmt.setString(6,Utilitario.getPropertyDynaBean(dyna,"otros"));
			pstmt.setString(7,Utilitario.getPropertyDynaBean(dyna,"modeloEstudio"));
			pstmt.setString(8,Utilitario.getPropertyDynaBean(dyna,"rxPeriapicales"));
			pstmt.setString(9,Utilitario.getPropertyDynaBean(dyna,"rxBitewing"));
			pstmt.setString(10,Utilitario.getPropertyDynaBean(dyna,"setFotografico"));
			pstmt.setString(11,Utilitario.getPropertyDynaBean(dyna,"rxPanoramica"));
			pstmt.setString(12,Utilitario.getPropertyDynaBean(dyna,"rxCefalometrica"));
			pstmt.setString(13,Utilitario.getPropertyDynaBean(dyna,"rxATM"));
			pstmt.setString(14,Utilitario.getPropertyDynaBean(dyna,"otrosPlantrabajo"));
			pstmt.setString(15,Utilitario.getPropertyDynaBean(dyna,"informeRadiografico"));
			pstmt.setInt(16,0);	
			pstmt.setString(17,"");	
			pstmt.executeUpdate();	
			ret = new BResult();
			ret.setCode(0);
		}catch(Exception e){
			ret = new BResult();
			ret.setCode(-1);
			ret.setMessage(e.getMessage());
		}finally{
			try{
				pstmt.close();
			}
			catch(Exception e){
				throw new DAOException(e.getMessage());	
			}
		}
		return ret;
	} 
}
